package com.lc.admin.config;

import com.lc.admin.mqtt.IMqttClient;
import com.lc.admin.mqtt.MqttClient;
import com.lc.admin.mqtt.MqttClientOptions;
import com.lc.admin.service.IotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.lc.admin.mqtt.MqttConstants.*;

@Configuration
public class MqttConfig {

    @Autowired
    IotService iotService;

    @Bean
    public IMqttClient mqttClient() {
        IMqttClient mqttClient = new MqttClient(MqttClientOptions.builder()
                .host(HOST)
                .port(PORT)
                .accessKey(ACCESS_KEY)
                .accessCode(ACCESS_CODE)
                .instanceId(INSTANCE_ID)
                .build());
        mqttClient.setRawMessageListener(message -> {
            iotService.processMessage(message);
        });
        mqttClient.connect();
        mqttClient.subscribeTopic(SUBSCRIBE_TOPIC, null);
        return mqttClient;
    }

}
